home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / tex / dvi / dvipssrc.zoo / finclude.c < prev    next >
C/C++ Source or Header  |  1990-12-13  |  8KB  |  322 lines

  1. /*
  2.  *  Code for allowing fonts to be used in PostScript files given via
  3.  *  `psfile=...'.
  4.  */
  5. #include "structures.h" /* The copyright notice in that file is included too! */
  6. #include <ctype.h>
  7. #ifndef SYSV
  8. extern char *strtok() ; /* some systems don't have this in strings.h */
  9. #endif
  10. #ifdef VMS
  11. #define getname vms_getname
  12. #endif
  13.  
  14. double atof();
  15. /*
  16.  *   These are the external routines we call.
  17.  */
  18. extern fontdesctype *newfontdesc() ;
  19. extern fontdesctype *matchfont() ;
  20. extern Boolean prescanchar() ;
  21. extern Boolean preselectfont() ;
  22. extern void scout() ;
  23. extern void stringend() ;
  24. extern void cmdout() ;
  25. extern void numout() ;
  26. extern void lfontout() ;
  27. extern char *newstring() ;
  28. extern FILE *search() ;
  29. /*
  30.  *   These are the external variables we access.
  31.  */
  32. extern fontdesctype *curfnt ;
  33. extern fontdesctype *fonthead ;
  34. extern integer fontmem ;
  35. extern fontdesctype *fonthd[MAXFONTHD] ;
  36. extern int nextfonthd ;
  37. extern char *nextstring ;
  38. extern char xdig[256] ;
  39. extern real conv ;
  40. extern integer pagecost ;
  41. extern int actualdpi ;
  42. extern Boolean includesfonts ;
  43. extern char *figpath ;
  44. /*
  45.  * Create a font descriptor for a font included in a psfile.  There will be
  46.  * no fontmaptype node for the resulting font descriptor until this font is
  47.  * encountered by fontdef() (if that ever happens).
  48.  */
  49. fontdesctype *
  50. ifontdef(name, area, scsize, dssize, scname)
  51. char *name, *scname, *area ;
  52. integer scsize, dssize ;
  53. {
  54.    fontdesctype *fp;
  55.  
  56.    fp = newfontdesc((integer)0, scsize, dssize, name, area);
  57.    fp->scalename = scname;
  58.    fp->next = fonthead ;
  59.    fonthead = fp ;
  60.    return fp;
  61. }
  62. /*
  63.  * When a font appears in an included psfile for the first time, this routine
  64.  * links it into the fonthd[] array.
  65.  */
  66. void
  67. setfamily(f)
  68. fontdesctype *f ;
  69. {
  70.    int i ;
  71.  
  72.    fontmem -= DICTITEMCOST;
  73.    for (i=0; i<nextfonthd; i++)
  74.       if (strcmp(f->name, fonthd[i]->name)==0
  75.             && strcmp(f->area, fonthd[i]->area)==0) {
  76.          f->nextsize = fonthd[i];
  77.          fonthd[i] = f;
  78.          return;
  79.       }
  80.    if (nextfonthd==MAXFONTHD)
  81.       error("! Too many fonts in included psfiles") ;
  82.    fontmem -= NAMECOST + strlen(f->name) + strlen(f->area) ;
  83.    fonthd[nextfonthd++] = f ;
  84.    f->nextsize = NULL ;
  85. }
  86. /*
  87.  * Convert file name s to a pair of new strings in the string pool.
  88.  * The first string is the original value of nextstring; the second
  89.  * string is the return value.
  90.  */
  91. char*
  92. getname(s)
  93. char *s ;
  94. {
  95.    char *a, *p, sav;
  96.  
  97.    a = NULL;
  98.    for (p=s; *p!=0; p++)
  99.       if (*p=='/')
  100.          a = p+1 ;
  101.    if (a==NULL) *nextstring++ = 0 ;
  102.    else {   sav = *a ;
  103.       *a = 0 ;
  104.       (void) newstring(s) ;
  105.       *a = sav ;
  106.       s = a ;
  107.    }
  108.    return newstring(s);
  109. }
  110. /*
  111.  * Mark character usage in *f based on the hexadicimal bitmap found in
  112.  * string s.  A two-digit offset separated by a colon gives the initial
  113.  * character code.  We have no way of knowing how many times each character
  114.  * is used or how many strings get created when showing the characters so
  115.  * we just estimate two usages per character and one string per pair of
  116.  * usages.
  117.  */
  118. void
  119. includechars(f, s)
  120. fontdesctype *f ;
  121. char *s ;
  122. {
  123.    int b, c, d ;
  124.    int l = strlen(s) ;
  125.  
  126.    if (l>0 && s[l-1]=='\n')
  127.       s[--l] = 0 ;
  128.    if (!isxdigit(s[0]) || !isxdigit(s[1]) || s[2]!=':'
  129.          || strspn(s+3,"0123456789ABCDEFabcdef") < l-3) {
  130.       fprintf(stderr, "%s\n", s) ;
  131.       error("Bad syntax in included font usage table") ;
  132.       return ;
  133.    }
  134.    c = (xdig[s[0]] << 4) + xdig[s[1]] ;
  135.    s += 2 ;
  136.    while (*++s) {
  137.       d = xdig[*s] ;
  138.       for (b=8; b!=0; b>>=1) {
  139.          if ((d&b)!=0) {
  140.             pagecost ++ ;
  141.             (void) prescanchar(&f->chardesc[c]) ;
  142.          }
  143.          if (++c==256) return ;
  144.       }
  145.    }
  146. }
  147. /*
  148.  * String p should be start after the ":" in a font declaration of the form
  149. %*FONT: <tfm-name> <scaled-size> <design-size> <2-hex-digits>:<hex-string>
  150.  * where the sizes are floating-point numbers in units of PostScript points
  151.  * (TeX's "bp").  We update the data structures for the included font,
  152.  * charge fontmem for the VM used, and add to delchar if necessary.
  153.  */
  154. void
  155. scan1fontcomment(p)
  156. char *p ;
  157. {
  158.    char *q, *name, *area;
  159.    char *scname;      /* location in buffer where we got scsize */
  160.    integer scsize, dssize;
  161.    fontdesctype *fptr;
  162.    real DVIperBP;
  163.  
  164.    DVIperBP = actualdpi/(72.0*conv);
  165.    p = strtok(p, " ");
  166.    if (p==NULL) return;
  167.    area = nextstring ;   /* tentatively in the string pool */
  168.    name = getname(p);
  169.    q = strtok((char *)0, " ");
  170.    if (p==NULL || (scsize=(int)(atof(q)*DVIperBP))==0) {
  171.       fprintf(stderr, "%s\n",p);
  172.       error("No scaled size for included font");
  173.       nextstring = area ;   /* remove from string pool */
  174.       return;
  175.    }
  176.    scname = q;
  177.    q = strtok((char *)0, " ");
  178.    if (p==NULL || (dssize=(int)(atof(q)*DVIperBP))==0) {
  179.       fprintf(stderr, "%s\n",p);
  180.       error("No design size for included font");
  181.       nextstring = area ;
  182.       return;
  183.    }
  184.    q = strtok((char *)0, " ");
  185.    fptr = matchfont(name, area, scsize, scname);
  186.    if (!fptr) {
  187.       fptr = ifontdef(name, area, scsize, dssize, newstring(scname));
  188.       (void) preselectfont(fptr);
  189.       setfamily(fptr);
  190.    } else {
  191.       nextstring = area;   /* remove from string pool */
  192.       (void) preselectfont(fptr);
  193.       if (fptr->scalename==NULL) {
  194.          fptr->scalename=newstring(scname);
  195.          setfamily(fptr);
  196.       }
  197.    }
  198.    includesfonts = 1;
  199.    fptr->psflag |= THISPAGE;
  200.    includechars(fptr, q);
  201. }
  202. /*
  203.  * Parse the arguments to a "%%VMusage" comment.  The Adobe Type 1 Font Format
  204.  * book specifies two arguments. This routine will accept one or two arguments;
  205.  * if there are two arguments we take the maximum.
  206.  */
  207. integer
  208. scanvm(p)
  209. char *p ;
  210. {
  211.    char* q;
  212.    integer vm, vmmax;
  213.    extern long atol() ;
  214.  
  215.    q = strtok(p, " ");
  216.    if (q==NULL) {
  217.       error("Missing data in VMusage comment");
  218.       return 0;
  219.    }
  220.    vmmax = atol(q);
  221.    q = strtok((char *)0, " ");
  222.    if (q!=NULL && (vm=atol(q))>vmmax)
  223.       vmmax = vm;
  224.    return vmmax;
  225. }
  226. /*
  227.  * Scan an initial sequence of comment lines looking for font and memory
  228.  * usage specifications.  This does not handle the "atend" construction.
  229.  */
  230. void
  231. scanfontcomments(filename)
  232. char* filename ;
  233. {
  234.    char p[500];
  235.    FILE *f;
  236.    integer truecost = pagecost ;
  237.    Boolean trueknown = 0 ;
  238.    fontdesctype *oldcf = curfnt;
  239.  
  240.    f = search(figpath, filename, READ) ;
  241.    if (f) {
  242.       while (fgets(p,500,f) && p[0]=='%' &&
  243.             (p[1]=='!' || p[1]=='%' || p[1]=='*'))
  244.          if (strncmp(p, "%*Font:", 7) == 0)
  245.             scan1fontcomment(p+7);
  246.          else if (strncmp(p, "%%VMusage:", 9) == 0) {
  247.             truecost += scanvm(p+10) ;
  248.             trueknown = 1 ;
  249.          }
  250.       if (trueknown)
  251.          pagecost = truecost ;
  252.       fclose(f) ;
  253.    }
  254.    curfnt = oldcf;
  255. }
  256. /*
  257.  * Is string s less than 30 characters long with no special characters
  258.  * that are not allowed in PostScript commands.
  259.  */
  260. Boolean
  261. okascmd(ss)
  262. char *ss ;
  263. {
  264.    register c = 0 ;
  265.    register char *s = ss ;
  266.  
  267.    while (*s)
  268.       if (*s<' ' || *s>126 || ++c==30)
  269.          return(0) ;
  270.    return(strcspn(ss,"()<>[]{}%/") == c) ;
  271. }
  272. /*
  273.  * Output font area and font name strings as a literal string
  274.  */
  275. void
  276. nameout(area, name)
  277. char *area, *name ;
  278. {
  279.    char buf[30] ;
  280.    char *s ;
  281.  
  282.    if (*area==0 && okascmd(name)) {
  283.       (void)sprintf(buf, "/%s", name) ;
  284.       cmdout(name);
  285.    } else {
  286.       for (s=area; *s; s++)
  287.          scout(*s) ;
  288.       for (s=name; *s; s++)
  289.          scout(*s) ;
  290.       stringend();
  291.       cmdout("cvn") ;
  292.    }
  293. }
  294. /*
  295.  * Output commands for defining a table of PostScript font identifiers for
  296.  * fonts used in included psfiles in the current section.
  297.  */
  298. void
  299. fonttableout()
  300. {
  301.    int i, k;
  302.    fontdesctype *f;
  303.  
  304.    for (i=0; i<nextfonthd; i++) {
  305.       for (f=fonthd[i]; f!=NULL; f=f->nextsize)
  306.          if (f->psflag==EXISTS) break;
  307.       if (f!=NULL) {
  308.          nameout(f->area, f->name);
  309.          k = 0;
  310.          do {   if (f->psflag==EXISTS) {
  311.                cmdout(f->scalename);
  312.                lfontout((int)f->psname);
  313.             }
  314.             f = f->nextsize;
  315.             k++;
  316.          } while (f!=NULL);
  317.          numout((integer)k);
  318.          cmdout("fstore");
  319.       }
  320.    }
  321. }
  322.